package cn.itcast.exception;

import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
* @PackageName:cn.itcast.exception
* 异常处理器
* @Class:SysExceptionResolver
* @author:ledehui
* @Date:2020/11/2 14:47
*/
public class SysExceptionResolver  implements HandlerExceptionResolver{

    /**
     * 处理异常业务逻辑
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param ex
     * @return
     */
    @Nullable
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @Nullable Object o, Exception ex) {
        //获取异常对象
        SysException e  = null;
        if (ex  instanceof  SysException){
            e = (SysException)ex;
        }else {
            e = new SysException("系统正在维护中.....");
        }
//      创建ModelAndView对象
        ModelAndView  modelAndView = new ModelAndView();
        modelAndView.addObject("errorMessage",e.getMessages());
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
